<template>
  <div>
    <el-form :inline="true" :model="searchForm" class="search-form">
      <el-form-item label="姓名">
        <el-input v-model="searchForm.name"></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="searchForm.age"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="searchForm.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="showDialog('add')">新增</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="text" @click="showDialog('edit', scope)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" :title="dialogTitle" width="30%">
      <el-form :model="formData" :rules="formRules" ref="form">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="formData.age"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="formData.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </el-dialog>

    <el-collapse v-model='activeName' >
      <el-collapse-item title='一致性 Consistency' name='1'>
        <div>与现实生活一致：与现实生活的流程、逻辑保持一致，遵循用户习惯的语言和概念；</div>
        <div>在界面中一致：所有的元素和结构需保持一致，比如：设计样式、图标和文本、元素的位置等。</div>
      </el-collapse-item>
      <el-collapse-item title='反馈 Feedback' name='2'>
        <div>控制反馈：通过界面样式和交互动效让用户可以清晰的感知自己的操作；</div>
        <div>页面反馈：操作后，通过页面元素的变化清晰地展现当前状态。</div>
      </el-collapse-item>

    </el-collapse>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeName: ['1', '2'] ,
      tableData: [
        { name: '张三', age: 20, gender: 1 },
        { name: '李四', age: 25, gender: 1 },
        { name: '王五', age: 30, gender: 2 },
      ],
      dialogVisible: false,
      dialogTitle: '',
      formData: {
        name: '',
        age: '',
        gender: '',
      },
      formRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
        gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
      },
      searchForm: {
        name: '',
        age: '',
        gender: '',
      },
    };
  },
  methods: {
    showDialog(type, row) {
      this.dialogVisible = true;
      if (type === 'add') {
        this.dialogTitle = '新增';
        this.formData = { name: '', age: '', gender: '' };
      } else if (type === 'edit') {
        this.dialogTitle = '编辑';
        this.formData = Object.assign({}, row);
      }
    },
    handleSubmit( ) {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.dialogTitle === '新增') {
            this.tableData.push(this.formData);
          } else if (this.dialogTitle === '编辑') {
            const index = this.tableData.findIndex((item) => item === row);
            this.tableData.splice(index, 1, this.formData);
          }
          this.dialogVisible = false;
        }
      });
    },
    handleDelete(row) {
      const index = this.tableData.findIndex((item) => item === row);
      this.tableData.splice(index, 1);
    },
    handleSearch() {
      // 根据查询条件重新请求数据
      // 示例代码中未实现，可根据具体需求自行实现
    },
  },
};
</script>

<style lang = "scss" scoped>
::v-deep .el-collapse-item__wrap:nth-last-child {
  border-width: 0;
}
</style>
